local CommonOHOH = _G.CommonOHOH;
local M = CommonOHOH.class.new(script, script.Parent.base,script.Parent.Name);
local HttpService=game:GetService("HttpService")

--服务器战斗对象

local ReayBattleCtrl=_G.server:GetComponent(_G.CommonOHOH.load.ReayBattleCtrl())


function M.new(self, ...)
	return M(self, ...);
end;

--参数：
--战斗地图
--关卡列表
--参加玩家列表 {系统Player1=true，系统Player2=true}
--战斗Id
function M.ctor(self,map,levelIdList,playerList,battleId)

    -- 添加特效生成文件夹
    local f=Instance.new("Folder",map)
    f.Name="Effect"
    -- 添加怪物生成文件夹
    local f2=Instance.new("Folder",map)
    f2.Name="Monster"

--    local f3=Instance.new("Folder",map)
--    f3.Name="players"
   --记录玩家信息
--    for player,v in pairs(playerList) do
--       local user=  Instance.new("BoolValue",f3)
--       user.Name=player.UserId
--       user.Value=true
--    end

    local userIdList={}
    for player,v in pairs(playerList) do
        userIdList[player.UserId]=true
    end

    map:SetAttribute("Players",  HttpService:JSONEncode(userIdList))

  
   --设置战斗Id
   map:SetAttribute("BattleId",battleId)

   --设置关卡信息
    map:SetAttribute("LevelIdList",HttpService:JSONEncode(levelIdList))


    M.super.ctor(self,map,levelIdList,playerList,battleId);

    self:SetWaveProgress(0,0,0,0)

    --设置地图父节点
    ReayBattleCtrl:SetBattleParent(map,battleId)

    --添加控制器
    self:AddPrivateComponentByFolder(script.Parent.ser)

    -- 让参与玩家进入战斗状态
    self:EnterBattle()

    self.ReayBattleCtrl=  _G.server:GetComponent(CommonOHOH.load.ReayBattleCtrl())
end;

-- 通知玩家进入战斗状态
function M.EnterBattle(self)
    for _,player in pairs(self:GetAllPlayerList()) do
        player.BattleSateEvent:Action(true,self)

        player.OnboardingFunnelEvent:Action("battle") --统计：进入战斗
    end

    self:Action()
end

--玩家退出战斗
function M.Exit(self,player)
      player.character:SetCFrame(self:GetExitCFrame())
      player.BattleSateEvent:Action(false)
      player.OnboardingFunnelEvent:Action("fail") --统计：战斗失败
      self:PlayerInstRemoveForBattle(player.main_inst)
end


function  M.GetExitCFrame(self)
    return self.ReayBattleCtrl:GetExitBattleCFrame(self:GetId())
end


--启动一场战斗
function M.Action(self)
    -- 开启刷怪
    self:GetPrivateComponent("MonsterCtrl"):Action()
end


--一场战斗胜利了
function M.Victory(self)
     for _,player in pairs(self:GetAllPlayerList()) do
        player.character:SetCFrame(self:GetExitCFrame())
        player.BattleSateEvent:Action(false)
        player:GetComponent(_G.CommonOHOH.load.Common_AttackCtrl()):Victory()
        self:End(true)
     end
end

function M.dtor(self)
    local inst= self.main_inst
	M.super.dtor(self);
    if not self.Destroyed then
        inst:Destroy()
        self.Destroyed=true
    end
end;


return M;
